package com.hmyd.hmyd_android.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dialog.ChangeNicknameDialog;
import com.hmyd.hmyd_android.dialog.ChangeNicknameDialogListener;
import com.hmyd.hmyd_android.dto.Activity;
import com.hmyd.hmyd_android.dto.Base;
import com.hmyd.hmyd_android.utils.MyToast;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 新手礼包
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_gift)
public class GiftActivity extends BasicActivity {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.newbieRegisterLayout)
	private View newbieRegisterLayout;
//	@ViewInject(R.id.newbieRegisterTitle)
//	private TextView newbieRegisterTitle;
//	@ViewInject(R.id.newbieRegisterTip)
//	private TextView newbieRegisterTip;
	@ViewInject(R.id.newbieRegisterButton)
	private Button newbieRegisterButton;

	private View dredgeAccountLayout;
//	private TextView dredgeAccountTitle;
//	private TextView dredgeAccountTip;
	private Button dredgeAccountButton;

	private View bindEmailLayout;
//	private TextView bindEmailTitle;
//	private TextView bindEmailTip;
	private Button bindEmailButton;

	private View modifyNicknameLayout;
//	private TextView modifyNicknameTitle;
//	private TextView modifyNicknameTip;
	private Button modifyNicknameButton;

	private String type;

	private final int ACTIVITYLIST = 0;
	private final int ACTIVITYPICK = 1;
	private final int CHANGENICKNAME = 2;

	private String nickName;

	@OnClick({ R.id.newbieRegisterButton, R.id.dredgeAccountButton, R.id.bindEmailButton,
			R.id.modifyNicknameButton })
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newbieRegisterButton:
			if (newbieRegisterButton.getTag() == null) {
				return;
			}

			if ("UnPick".equals(String.valueOf(newbieRegisterButton.getTag()))) {
				type = "NewbieRegister";
				pick();
			}

			break;
		case R.id.dredgeAccountButton:

			if (dredgeAccountButton.getTag() == null) {
				return;
			}

			if ("UnPick".equals(String.valueOf(dredgeAccountButton.getTag()))) {
				type = "DredgeAccount";
				pick();
			} else if ("UnActivate".equals(String.valueOf(dredgeAccountButton.getTag()))) {
				startActivity(new Intent(this, CreateAccountActivity.class));
			}

			break;
		case R.id.bindEmailButton:

			if (bindEmailButton.getTag() == null) {
				return;
			}

			if ("UnPick".equals(String.valueOf(bindEmailButton.getTag()))) {
				type = "BindEmail";
				pick();
			} else if ("UnActivate".equals(String.valueOf(bindEmailButton.getTag()))) {
				startActivity(new Intent(this, MailVerifyActivity.class));
			}

			break;
		case R.id.modifyNicknameButton:

			if (modifyNicknameButton.getTag() == null) {
				return;
			}

			if ("UnPick".equals(String.valueOf(modifyNicknameButton.getTag()))) {
				type = "ModifyNickname";
				pick();
			} else if ("UnActivate".equals(String.valueOf(modifyNicknameButton.getTag()))) {
				ChangeNicknameDialog changeNicknameDialog = new ChangeNicknameDialog(
						GiftActivity.this, R.style.calculatorDialog,
						new ChangeNicknameDialogListener() {
							@Override
							public void getIdentifyind(String name) {
								nickName = name;

								RequestParams params = new RequestParams();
								params.addBodyParameter("nickname", name);
								onRequest(CHANGENICKNAME, UrlConstant.CHANGENICKNAME,
										HttpMethod.POST, params);
							}
						});

				changeNicknameDialog.show();
			}

			break;
		default:
			break;
		}

	}

	private void pick() {
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", type);
		onRequest(ACTIVITYPICK, UrlConstant.ACTIVITYPICK, HttpMethod.POST, params);
	}

	@Override
	protected void onResume() {
		super.onResume();
		onRequest(ACTIVITYLIST, UrlConstant.ACTIVITYLIST, HttpMethod.GET, null);
	}

	private void initView() {

		dredgeAccountLayout = findViewById(R.id.dredgeAccountLayout);
//		dredgeAccountTitle = (TextView) findViewById(R.id.dredgeAccountTitle);
//		dredgeAccountTip = (TextView) findViewById(R.id.dredgeAccountTip);
		dredgeAccountButton = (Button) findViewById(R.id.dredgeAccountButton);

		bindEmailLayout = findViewById(R.id.bindEmailLayout);
//		bindEmailTitle = (TextView) findViewById(R.id.bindEmailTitle);
//		bindEmailTip = (TextView) findViewById(R.id.bindEmailTip);
		bindEmailButton = (Button) findViewById(R.id.bindEmailButton);

		modifyNicknameLayout = findViewById(R.id.modifyNicknameLayout);
//		modifyNicknameTitle = (TextView) findViewById(R.id.modifyNicknameTitle);
//		modifyNicknameTip = (TextView) findViewById(R.id.modifyNicknameTip);
		modifyNicknameButton = (Button) findViewById(R.id.modifyNicknameButton);

	}

	@Override
	public void onPostSuccess(int postId, String result) {

		Gson gson = new Gson();

		switch (postId) {
		case ACTIVITYLIST:
			java.lang.reflect.Type type = new TypeToken<List<Activity>>() {
			}.getType();
			List<Activity> activityList = gson.fromJson(result, type);

			for (Activity activity : activityList) {
				if (activity.type.equals("NewbieRegister")) {
					newbieRegisterLayout.setVisibility(View.VISIBLE);
//					newbieRegisterTitle.setText(activity.title);
//					newbieRegisterTip.setText(activity.awardName);
					if (activity.status.equals("Picked")) {
						newbieRegisterButton.setEnabled(false);
						newbieRegisterButton.setBackgroundResource(R.drawable.xin_img_hui);
						newbieRegisterButton.setText("已领取");
					} else if (activity.status.equals("UnPick")) {
						newbieRegisterButton.setEnabled(true);
						newbieRegisterButton.setText("立即领取");
					} else if (activity.status.equals("UnActivate")) {
						newbieRegisterButton.setEnabled(true);
						newbieRegisterButton.setText("立即注册");
					}
					newbieRegisterButton.setTag(activity.status);

				} else if (activity.type.equals("DredgeAccount")) {
					dredgeAccountLayout.setVisibility(View.VISIBLE);
//					dredgeAccountTitle.setText(activity.title);
//					dredgeAccountTip.setText(activity.awardName);
					if (activity.status.equals("Picked")) {
						dredgeAccountButton.setEnabled(false);
						dredgeAccountButton.setText("已领取");
						dredgeAccountButton.setBackgroundResource(R.drawable.xin_img_hui);
					} else if (activity.status.equals("UnPick")) {
						dredgeAccountButton.setEnabled(true);
						dredgeAccountButton.setText("立即领取");
					} else if (activity.status.equals("UnActivate")) {
						dredgeAccountButton.setEnabled(true);
						dredgeAccountButton.setText("立即开户");
					}
					dredgeAccountButton.setTag(activity.status);

				} else if (activity.type.equals("BindEmail")) {
					bindEmailLayout.setVisibility(View.VISIBLE);
//					bindEmailTitle.setText(activity.title);
//					bindEmailTip.setText(activity.awardName);
					if (activity.status.equals("Picked")) {
						bindEmailButton.setEnabled(false);
						bindEmailButton.setText("已领取");
						bindEmailButton.setBackgroundResource(R.drawable.xin_img_hui);
					} else if (activity.status.equals("UnPick")) {
						bindEmailButton.setEnabled(true);
						bindEmailButton.setText("立即领取");
					} else if (activity.status.equals("UnActivate")) {
						bindEmailButton.setEnabled(true);
						bindEmailButton.setText("立即激活");
					}
					bindEmailButton.setTag(activity.status);

				} else if (activity.type.equals("ModifyNickname")) {
					modifyNicknameLayout.setVisibility(View.VISIBLE);
//					modifyNicknameTitle.setText(activity.title);
//					modifyNicknameTip.setText(activity.awardName);
					if (activity.status.equals("Picked")) {
						modifyNicknameButton.setEnabled(false);
						modifyNicknameButton.setText("已领取");
						modifyNicknameButton.setBackgroundResource(R.drawable.xin_img_hui);
					} else if (activity.status.equals("UnPick")) {
						modifyNicknameButton.setEnabled(true);
						modifyNicknameButton.setText("立即领取");
					} else if (activity.status.equals("UnActivate")) {
						modifyNicknameButton.setEnabled(true);
						modifyNicknameButton.setText("立即修改");
					}
					modifyNicknameButton.setTag(activity.status);
				}
			}

			break;
		case CHANGENICKNAME:
			AppBasic.nickname = nickName;
			modifyNicknameButton.setEnabled(true);
			modifyNicknameButton.setText("立即领取");
			modifyNicknameButton.setTag("UnPick");

			break;
		case ACTIVITYPICK:
			java.lang.reflect.Type type2 = new TypeToken<Base>() {
			}.getType();
			Base base = gson.fromJson(result, type2);
			MyToast.showHMToast(this, true, base.info);

			onRequest(ACTIVITYLIST, UrlConstant.ACTIVITYLIST, HttpMethod.GET, null);
			break;
		default:
			break;
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_title_txt.setText("新手礼包");
		home_title.setOnTouchListener(touch);
		initView();
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

}
